import {
    wxRequest
} from '../../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()

Page({
    data: {
        imgUrl: app.globalData.uploadsUrl,
        infoList: [],

        auditTypeList: [{
                key: 1,
                value: '审核通过'
            },
            {
                key: 2,
                value: '审核驳回'
            },
        ],
        auditTypeIndex: 0,
        auditType: 1,
    },

    onLoad: function (options) {
        let list = wx.getStorageSync('auditItem')
        console.log(123);
        for (const key in list) {
           if (typeof list[key] === 'string') {
               list[key] = list[key].trim()
           }
        }
        this.data.infoList = list

        if (this.data.infoList.idcardBackPath !== '') {
            this.data.infoList.idcardBackPath = this.data.imgUrl + this.data.infoList.idcardBackPath
        }
        if (this.data.infoList.idcardFrontPath !== '') {
            this.data.infoList.idcardFrontPath = this.data.imgUrl + this.data.infoList.idcardFrontPath
        }
        this.setData({
            infoList: this.data.infoList,
            auditTypeList: this.data.auditTypeList
        })
    },

    // 审核
    formSubmit: function (e) {
        var label = e.detail.target.dataset.label
        var formdata = e.detail.value;
        var auditStatus = ''
        label === 'tongguo' ? auditStatus = 1 : auditStatus = 2

        var url = '/odr/protected/account/auditAccount'
        var title = "审核完成"
        var params = {}

        if (auditStatus === 2) {
            params = {
                ...formdata,
                auditStatus,
                id: this.data.infoList.id
            }
        } else {
            params = {
                auditStatus,
                id: this.data.infoList.id
            }
        }
        console.log(params);

        if (auditStatus === 2 && formdata.auditMsg === '') {
            wx.showToast({ 
                title: '驳回需填写信息',
                icon: 'error',
            })
        }

        _wxRequest.wxTonkenAjax({
            url: app.globalData.apiUrl + url,
            method: 'POST',
            data: params
        }, (res) => {
            console.log('审核结果：', res)

            wx.showToast({
                title
            })
            setTimeout(() => {
                wx.redirectTo({
                    url: '/pages/finance_account/account_audit/account_audit',
                })
            }, 1500)
        })
    },

    // 使用条件类型
    pickerChangeCondition: function (e) {
        var index = e.detail.value;
        var auditTypeList = this.data.auditTypeList;
        var auditType = ''
        auditTypeList.map((item, i) => {
            if (i == index) {
                auditType = item.key
            }
        })

        this.setData({
            auditTypeIndex: index,
            auditType
        })
    },

    imgView: function (e) {
        var src = e.target.dataset.url; //获取data-src
        var imgList = [e.target.dataset.url]; //获取data-list
        //图片预览
        wx.previewImage({
            current: src, // 当前显示图片的http链接
            urls: imgList // 需要预览的图片http链接列表
        })
    },

    onUnload: function () {
        wx.removeStorageSync('auditItem')
    },

    onHide: function () {},

    onShow: function () {

    },

})